package com.luhuiguo.cms.server.handler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Component;

import com.luhuiguo.cms.server.entity.Article;
import com.luhuiguo.cms.server.service.ArticleService;
import com.luhuiguo.cms.server.util.MapperUtils;
import com.luhuiguo.cms.shared.model.ArticleModel;
import com.luhuiguo.cms.shared.rpc.UpdateArticle;
import com.luhuiguo.cms.shared.rpc.UpdateArticleResult;
import com.luhuiguo.gwt.dispatcher.server.ActionHandler;
import com.luhuiguo.gwt.dispatcher.server.ExecutionContext;
import com.luhuiguo.gwt.dispatcher.shared.exception.ActionException;

@Component
public class UpdateArticleHandler implements ActionHandler<UpdateArticle, UpdateArticleResult> {


    @Autowired
    private ArticleService articleService;

    @Override
    @PreAuthorize("isAuthenticated()")
    public UpdateArticleResult execute(UpdateArticle action, ExecutionContext context) throws ActionException {
        Article a = MapperUtils.map(action.get(), Article.class);
        articleService.updateArticle(a);
        return new UpdateArticleResult(action.get(), MapperUtils.map(a, ArticleModel.class));
    }

    @Override
    public Class<UpdateArticle> getActionType() {
        return UpdateArticle.class;
    }

    @Override
    public void rollback(UpdateArticle action, UpdateArticleResult result, ExecutionContext context)
            throws ActionException {

    }

}
